package hub.cathome.controller;

import hub.annotation.View;
import hub.cathome.entity.ShotBlog;
import hub.cathome.entity.User;
import hub.cathome.service.ShotBlogService;
import hub.cathome.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class WatcherController {
    @Resource(name="userService")
    private UserService userService;
    @Resource(name="shotBlogService")
    private ShotBlogService shotBlogService;
    @View("观察用户主页视图")
    @GetMapping("/look/{userId}")
    public String watchUserHome(@PathVariable String userId, Model model){
        Integer otherUserId;
        try{
            otherUserId = Integer.parseInt(userId);
        }catch (Exception e){
            model.addAttribute("error",e.toString());
            return "error/error";
        }
        User otherUser = userService.getUserByUserId(otherUserId);
        if(otherUser!=null){
            otherUser.setPassword(null);
        }else{
            return "look/not_find";
        }
        List<ShotBlog> shotBlogList = shotBlogService.getShotBlogByUserId(otherUserId);
        model.addAttribute("other",otherUser);
        model.addAttribute("otherShotBlogList",shotBlogList);
        return "look/other";
    }

}
